//
//  WPCLeftSidebarViewController.swift
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/22.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

import UIKit

class WPCLeftSidebarViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, JYCarouselDelegate{
    private var leftSidebarView: WPCLeftSidebarView?;
    private var presenter: WPCLeftSidebarPresenter?;
    
    
    init() {
        super.init(nibName: nil, bundle: nil);
        presenter = WPCLeftSidebarPresenter();
        presenter?.attachController(ctler: self);
        
        //self.view.backgroundColor = UIColor.gray.withAlphaComponent(0.3);
        self.modalPresentationStyle = UIModalPresentationStyle.custom;
        
        leftSidebarView = WPCLeftSidebarView.init(frame: self.view.bounds, delegate: self);
        self.view.addSubview(leftSidebarView!);
        
        self.layoutWithMasonry();
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setDelegate(delegate:WPCHomepageViewController) {
        presenter?.homepage = delegate;
    }
    
    override func viewDidLoad() {
        presenter?.refreshBannerModel(rBlock: { (result) in
            if result {
                /*更新banner信息*/
                self.leftSidebarView?.reloadBanners(banners: self.presenter?.getBannersArray());
            }
        })
        
        presenter?.refreshCategoryModel(rBlock: { (result) in
            if result {
                /*更新category信息*/
                self.leftSidebarView?.reloadCategorys();
            }
        })
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);        
        self.view.backgroundColor = UIColor.gray.withAlphaComponent(0);
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        self.view.backgroundColor = UIColor.gray.withAlphaComponent(0.3);
    }
    
    //MARK:- private method
    func layoutWithMasonry() {
        leftSidebarView!.makeConstraints({ (make:MASConstraintMaker?) in
            make!.left.equalTo()(self.view);
            make!.top.equalTo()(self.view);
            make!.height.equalTo()(self.view);
            make!.width.equalTo()(self.view)!.multipliedBy()(0.8);
        })
    }
    //MARK:- event
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first;
        if self.view != touch?.view {
            return;
        }
        
        self.dismiss(animated: false, completion: nil);
    }
    
    //MARK:- UICollectionViewDataSource
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return (presenter?.sectionCount)!;
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (presenter?.numberOfItemsIn(section: section))!;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = leftSidebarView?.dequeueReusableCell(indexPath: indexPath, withItem:(presenter?.getCategoryItemWith(section: indexPath.section, index: indexPath.row))!);
        return cell!;
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        return (leftSidebarView?.dequeueReusableView(kind: kind, indexPath: indexPath))!;
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        presenter?.didSelectCategoryItem(indexPath: indexPath);
        /* 弹出自己 */
        self.dismiss(animated: false, completion: nil);
    }
    
    //MARK:- JYCarouselDelegate
    func carouselViewClick(_ index: Int) {
        print("carouselViewClick>[%d]\r\n", index);
    }
}
